package cn.anc.aonicardv.widget;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.TypedValue;
import android.view.View;

import java.util.List;

import androidx.recyclerview.widget.RecyclerView;
import cn.anc.aonicardv.R;

import static android.util.TypedValue.COMPLEX_UNIT_DIP;
import static android.util.TypedValue.COMPLEX_UNIT_SP;

/**
 * Created by yangdai on 2017/6/3.
 */

public class AlbumDecoration extends RecyclerView.ItemDecoration {

    private Paint paint;
    public static final int DECORATION_HEIGHT = 30;
    public static final int DECORATION_TEXT_SIZE = 16;
    private List<DecorationBean> decorationBeans;
    private Rect rect;

    public AlbumDecoration(List<DecorationBean> decorationBeans) {
        this.decorationBeans = decorationBeans;
        init();
    }

    private void init() {
        paint = new Paint();
        rect = new Rect();
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if (decorationBeans == null)
            return;
        for (int j = 0; j < decorationBeans.size(); j++) {
            for (int i = 0; i < parent.getChildCount(); i++) {
                int position = parent.getChildLayoutPosition(parent.getChildAt(i));
                if (position == decorationBeans.get(j).position) {
                    View v = parent.getChildAt(i);
                    RecyclerView.LayoutParams l = (RecyclerView.LayoutParams) v.getLayoutParams();
                    float height = TypedValue.applyDimension(COMPLEX_UNIT_DIP, DECORATION_HEIGHT, parent.getContext().getResources().getDisplayMetrics());
                    paint.setColor(Color.WHITE);
                    c.drawRect(parent.getLeft(), v.getTop() + l.topMargin - height, parent.getRight(), v.getTop() + l.topMargin, paint);
                    drawText(c, j, parent.getLeft(), v.getTop() + l.topMargin - height, height, parent);
                    break;
                }
            }
        }
    }

    private void drawText(Canvas c, int index, int x, float y, float height, RecyclerView parent) {
        if (decorationBeans.size() == 0)
            return;
        float textSize = TypedValue.applyDimension(COMPLEX_UNIT_SP, DECORATION_TEXT_SIZE, parent.getContext().getResources().getDisplayMetrics());
        rect.set(0, 0, 0, 0);
        paint.setColor(Color.BLACK);
        paint.setTextSize(textSize);
        paint.getTextBounds(decorationBeans.get(index).text, 0, decorationBeans.get(index).text.length(), rect);
        Bitmap bitmap = BitmapFactory.decodeResource(parent.getResources(), R.mipmap.icon_date);
        c.drawBitmap(bitmap, x + 20, y + height / 2 - bitmap.getHeight()/2, paint);
        c.drawText(decorationBeans.get(index).text, 0, decorationBeans.get(index).text.length(), x + bitmap.getWidth()+40, y + height / 2 + rect.height() / 2, paint);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        int p = parent.getChildAdapterPosition(view);
        float height = TypedValue.applyDimension(COMPLEX_UNIT_DIP, DECORATION_HEIGHT, parent.getContext().getResources().getDisplayMetrics());
        for (int i = 0; i < decorationBeans.size(); i++) {
            DecorationBean d = decorationBeans.get(i);
            if (p == d.position || p == d.position + 1 || p == d.position + 2) {
                outRect.set(0, (int) height, 0, 0);
                break;
            }
        }
    }


    public static class DecorationBean {

        public int position;
        public String text;
        public int itemCount;

        public DecorationBean(int position, String text, int itemCount) {
            this.position = position;
            this.text = text;
            this.itemCount = itemCount;
        }
    }
}
